﻿namespace Api
{
    public abstract class EditingBaseController<TModel, TFilterModel, TILogic> : AuthBaseController
        where TModel : BaseModel
        where TFilterModel : BaseFilterModel
        where TILogic : ILogic<TModel, TFilterModel>
    {
        public readonly ICurrentUser _currentUser;
        public readonly TILogic _logic;

        public EditingBaseController(ICurrentUser currentUser, TILogic logic)
        {
            _currentUser = currentUser;
            _logic = logic;
        }

        #region 添加  Insert  Post
        /// <summary>
        /// 添加
        /// </summary>
        [HttpPost]
        public virtual async Task<TModel> Insert([FromBody] TModel model)
        {
            return await Task.Run(() => _logic.Insert(model));
        }
        #endregion

        #region 批量添加  BatchInsert  Post
        /// <summary>
        /// 批量添加
        /// </summary>
        [HttpPost]
        public virtual async Task<int> BatchInsert([FromBody] List<TModel> models)
        {
            return await Task.Run(() => _logic.BatchInsert(models));
        }
        #endregion



        #region 修改  Update  Put
        /// <summary>
        /// 修改
        /// </summary>
        [HttpPut]
        public virtual async Task<int> Update([FromBody] TModel model)
        {
            return await Task.Run(() => _logic.Update(model));
        }
        #endregion

        #region 批量修改  BatchUpdate  Put
        /// <summary>
        /// 批量修改
        /// </summary>
        [HttpPut]
        public virtual async Task<int> BatchUpdate([FromBody] List<TModel> models)
        {
            return await Task.Run(() => _logic.BatchUpdate(models));
        }
        #endregion



        #region 物理删除  Delete  Delete
        /// <summary>
        /// 物理删除
        /// </summary>
        [HttpDelete]
        [Route("{id}")]
        public virtual async Task<int> Delete(int id)
        {
            return await Task.Run(() => _logic.Delete(id));
        }
        #endregion

        #region 批量物理删除  BatchDelete  Delete
        /// <summary>
        /// 批量物理删除
        /// </summary>
        [HttpDelete]
        public virtual async Task<int> BatchDelete([FromBody] List<int> autoIDs)
        {
            return await Task.Run(() => _logic.BatchDelete(autoIDs));
        }
        #endregion



        #region 根据ID得到对象  GetByID  Get
        /// <summary>
        /// 根据ID得到对象
        /// </summary>
        [HttpGet]
        [Route("{id}")]
        public virtual async Task<TModel> GetByID(int id)
        {
            return await Task.Run(() => _logic.GetByID(id));
        }
        #endregion

        #region 自定义查询对象  GetModel  Post
        /// <summary>
        /// 自定义查询对象
        /// </summary>
        [HttpPost]
        public virtual async Task<TModel> GetModel([FromBody] TFilterModel filterModel, string orderBy = null)
        {
            return await Task.Run(() => _logic.GetModel(filterModel, orderBy));
        }
        #endregion

        #region 得到所有数据  GetAll  Get
        /// <summary>
        /// 得到所有数据
        /// </summary>
        [HttpGet]
        public virtual async Task<IEnumerable<TModel>> GetAll(string orderBy = null)
        {
            return await Task.Run(() => _logic.GetAll(orderBy));
        }
        #endregion

        #region 自定义搜索  GetSearch  Post
        /// <summary>
        /// 自定义搜索
        /// </summary>
        [HttpPost]
        public virtual async Task<IEnumerable<TModel>> GetSearch([FromBody] TFilterModel filterModel, string orderBy = null, int? top = null)
        {
            return await Task.Run(() => _logic.GetSearch(filterModel, orderBy, top));
        }
        #endregion

        #region 分页查询  GetPage  Post
        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpPost]
        public virtual async Task<PageModel<TModel>> GetPage([FromBody] TFilterModel filterModel, int pageIndex = 1, int pageSize = Gx._PAGE_SIZE, string orderBy = null)
        {
            return await Task.Run(() => _logic.GetPage(filterModel, pageIndex, pageSize, orderBy));
        }
        #endregion

        #region 自定义条件返回数量  GetCount  Post
        /// <summary>
        /// 自定义条件返回数量
        /// </summary>
        [HttpPost]
        public virtual async Task<int> GetCount([FromBody] TFilterModel filterModel)
        {
            return await Task.Run(() => _logic.GetCount(filterModel));
        }
        #endregion
    }
}
